package org.hang.jvm.classpath.impl;

import org.hang.jvm.classpath.Entry;

import java.io.IOException;
import java.nio.file.*;

/**
 * 表示ZIP或者JAR文件形式的类路径
 *
 * @author 廖景航
 * @date created on 2022/2/23 下午4:00
 */
public class ZipEntry implements Entry {

    private Path absolutePath;

    public ZipEntry(String path) {
        // 获取绝对路径
        this.absolutePath = Paths.get(path).toAbsolutePath();
    }

    @Override
    public byte[] readClass(String classname) throws IOException {
        try (FileSystem zipFs = FileSystems.newFileSystem(absolutePath, null)) {
            return Files.readAllBytes(zipFs.getPath(classname));
        }
    }

    @Override
    public String toString() {
        return "ZipEntry{" +
                "absolutePath=" + absolutePath +
                '}';
    }
}
